package cn.edu.neu.controller.manage;
import javax.servlet.http.HttpServletRequest;


import cn.edu.neu.model.Category;
import cn.edu.neu.service.CategoryService;
import cn.edu.neu.utils.FileUploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


/**
 * 商品分类管理控制器类
 */
@Controller
@RequestMapping("/manage/cate")
public class CategoryController {
	// 依赖注入
	@Autowired
	private CategoryService categoryService;

	/**
	 *  商品分类信息
	 */
	@RequestMapping(value = "getCateList")
	public String getCatelistForPage( @RequestParam(value = "page", defaultValue = "1") int page,
									  @RequestParam(value = "count", defaultValue = "6") int count,
									  HttpServletRequest request) {
		PageInfo<Category> cateListPage=categoryService.findCateList(page,count);
		request.setAttribute("catelist",cateListPage);
		return "manage/cate/category_list";
	}

	/**
	 * 更新商品分类
	 */
	@PostMapping("updateCate")
	@ResponseBody
	public Map updateCate(Category category,MultipartFile uploadfile) {
		Map upload=new HashMap<>();
		System.out.println("------"+uploadfile);
		if(!uploadfile.isEmpty()){
			String path="/static/images/cate/";
			upload= FileUploadUtil.uploadFile(uploadfile,path);
            category.setCate_pic(upload.get("cate_pic").toString());
			System.out.println(upload.get("cate_pic").toString());
		}

		int rows = categoryService.updateCate(category);

		Map map=new HashMap<>();
		if(rows > 0){
			if(!upload.isEmpty()&&upload.get("msg").equals("success")){
				map.put("result","success");
				map.put("msg","商品分类修改成功");
			}else if(!upload.isEmpty()&&upload.get("msg").equals("fail")){
				map.put("result","success");
				map.put("msg","商品分类名称修改成功,分类图片上传失败");
		    }else{
				map.put("result","success");
				map.put("msg","商品分类名称修改成功,分类图片未修改");
			}
		}else{
			map.put("result","fail");
			map.put("msg","商品分类修改失败");
		}
		return map;

	}

	/**
	 * 删除商品分类信息
	 */
	@RequestMapping("deleteCate/{cateid}")
	public String deleteCate(@PathVariable("cateid") Integer cate_id) {
		int rows = categoryService.deleteCate(cate_id);
		return "redirect:../getCateList";
	}


	/**
	 * 新建商品分类
	 */
	@PostMapping("addCate")
	@ResponseBody
	public Map addCate(Category cate,MultipartFile uploadfile,
						  HttpServletRequest request) {
		System.out.println("=========dirPath=1111");
		String path="/static/images/cate/";
		Map upload= FileUploadUtil.uploadFile(uploadfile,path);

		cate.setCate_pic(upload.get("cate_pic").toString());
	    int rows = categoryService.createCategory(cate);

		Map map=new HashMap<>();
		if(rows > 0&&upload.get("msg").equals("success")){
			map.put("result","success");
			map.put("msg","商品分类创建成功");
		}else if(rows > 0&&upload.get("msg").equals("fail")){
			map.put("result","success");
			map.put("msg","商品分类创建成功,但分类图片上传失败");
		}else{
			map.put("result","fail");
			map.put("msg","商品分类创建失败");
		}
		return map;
	}

}
